package com.commont;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
 * 图灵机器人
 */
public class Tuling {
    public static String send(String question)  {
        String APIKEY = "24561e519ea244faaa4f1816188cf4b3";
        HttpURLConnection connection = null;
        StringBuffer sb = null;
        try {
            //String question = "南京";//这是上传给云机器人的问题
            //String INFO = URLEncoder.encode("北京今日天气", "utf-8");
            String INFO = URLEncoder.encode(question, "utf-8");
            String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO;
            URL getUrl = new URL(getURL);
            connection = (HttpURLConnection) getUrl.openConnection();
            connection.connect();

            // 取得输入流，并使用Reader读取
            BufferedReader reader = new BufferedReader(new InputStreamReader ( connection.getInputStream(), "utf-8"));
            sb = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 断开连接
        connection.disconnect();
       return sb.toString ();
    }

    public static void main(String[] args) {
        try {
            System.out.println(send("合肥的天气"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
